#coding:utf-8
import sys, os, re
sys.path.append("matchutils")
from config import *
import jieba


try:
    dicfile = dict_dir
    if not dicfile.endswith('/'):
        dicfile += '/'
    dicfile += 'userdict.txt'
    jieba.load_userdict( dicfile )
    del dicfile
except Exception,e:
    pass

import jieba.posseg as pseg


biaodian = {}
biaodian['。'] = 1
biaodian['.'] = 1
biaodian['；'] = 1
biaodian['【'] = 1
biaodian['】'] = 1
biaodian[';'] = 1
biaodian[','] = 1
biaodian['，'] = 1
biaodian['｛'] = 1
biaodian['｝'] = 1
biaodian['（'] = 1
biaodian['）'] = 1
biaodian['('] = 1
biaodian[')'] = 1
biaodian['!'] = 1
biaodian['！'] = 1
biaodian['“'] = 1
biaodian['”'] = 1
biaodian['‘'] = 1
biaodian['’'] = 1
biaodian['‘'] = 1
biaodian['《'] = 1
biaodian['》'] = 1
biaodian['、'] = 1

_roma = {}
_roma['Ⅰ'] = 100
_roma['Ⅱ'] = 100
_roma['Ⅲ'] = 100
_roma['Ⅳ'] = 100
_roma['Ⅴ'] = 100
_roma['Ⅵ'] = 100
_roma['Ⅶ'] = 100
_roma['Ⅷ'] = 100
_roma['Ⅸ'] = 100
_roma['Ⅹ'] = 100
_roma['Ⅺ'] = 100
_roma['Ⅻ'] = 100
_roma['ⅺ'] = 100
_roma['ⅻ'] = 100
_roma['ⅸ'] = 100
_roma['ⅹ'] = 100
_roma['ⅲ'] = 100
_roma['ⅳ'] = 100
_roma['ⅰ'] = 100
_roma['ⅱ'] = 100
_roma['ⅶ'] = 100
_roma['ⅷ'] = 100
_roma['ⅴ'] = 100
_roma['ⅵ'] = 100

_roma['i'] = 100
_roma['ii'] = 100
_roma['iii'] = 100
_roma['iv'] = 100
_roma['vi'] = 100
_roma['vii'] = 100
_roma['viii'] = 100
_roma['v'] = 100
_roma['xi'] = 100
_roma['xii'] = 100
_roma['xi'] = 100

def cutwords( line ):
    if line is None:
        return []
    line = line.strip()
    if not line : 
        return []

    results = []
    words = pseg.cut( line )
    i = 0
    for word in words:
        item = {}
        curword = word.word
        item['word'] = word.word.encode('utf-8', 'ignore').strip()
        item['flag'] = word.flag.encode('utf-8', 'ignore')
        if item['word'] in _roma:
            item['flag'] = 'm'
        elif item['flag'] == 'x' or item['flag'] == 'w':
            if item['word'].isdigit(): 
                item['flag'] = 'm'
            elif item['word'].isalnum(): 
                item['flag'] = 'd'
        if item['word'] in biaodian: 
            item['flag'] = 'w'
        if i > 0 and item['word'] in '一二三四五六七八九' and results[i-1]['flag'] == "m"  and len(results[i-1]['word']) <= 9:
            tmpword = results[i-1]['word'].decode('utf-8', 'ignore')
            if re.search(u'^[零一二三四五六七八九]{1,3}$', tmpword ):
                results[-1]['word'] += item['word']
                continue
				
        #中字 无字
        if i > 0 and item['word'] == '字':
            if results[-1]['word'] == '无':
                results[-1]['word'] += '字'
                continue
            elif results[-1]['word'] == '无':
                results[-1]['word'] += '字'
                continue
        if i > 0 and item['word'] == '版':
            if results[-1]['word'] == 'dvd':
                results[-1]['word'] += '版'
            elif results[-1]['word'] == 'tv':
                results[-1]['word'] += '版'
        if i > 0 and item['word'] == '篇':
            if len(results[-1]['word']) <= 12:
                results[-1]['word'] += '篇'
        if i > 0 and item['word'] == 'd':
            if results[-1]['word'] == '3':
                results[-1]['word'] += 'd'
                results[-1]['flag'] += 'n'
                continue
            elif results[-1]['word'] == '2':
                results[-1]['word'] += 'd'
                results[-1]['flag'] += 'n'
                continue
        if item['word']  == '':
            if len(results) == 0: continue
            if item['word'] == results[-1]['word']: continue

        results.append( item )
        i += 1

    return results 


def cut_video_title():
    for line in sys.stdin:
        line = line.strip()
        if line == '': continue
        line = simple2tradition(line)
        line = tradition2simple(line)
        line = line.lower()
        words = cutwords(line)
        allwords = [ word['word']  for word in words]
        print ' '.join(allwords)
        


if __name__ == "__main__":
    #for key in _roma:
    #    key = key.lower()
    #    print key
    #    cutwords(key)
    #cut_video_title()
    line = '二十四小时八点'
    line = '文豪ストレイドッグス'
    line = '高三3班的秘密'
    words = cutwords(line)

    for word in words:
        print word['word'], word['flag']



